package com.pan.controller;

import com.pan.entity.Result;
import com.pan.service.UserService;
import com.pan.vo.UserRegisterVo;
import com.pan.vo.UserVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;

@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @ResponseBody
    @PostMapping("/login")
    public Result<String> login(HttpServletRequest request, HttpServletResponse response,
                        @RequestParam Long tel, @RequestParam String password){
        Result<String> result = userService.toLogin(request, response, tel, password);
        return result;
    }

    @ResponseBody
    @PostMapping("/logoff")
    public Result logoff(HttpServletResponse response,
                         @CookieValue("userTicket") String userTicket){

        Result result = userService.logoff(response, userTicket);

        return result;
    }

    @ResponseBody
    @PostMapping("/register")
    public Result register(@RequestBody UserRegisterVo userRegisterVo){
        Result result = userService.register(userRegisterVo);
        return result;
    }

    @ResponseBody
    @GetMapping("/select/tel/{tel}")
    public Result<UserVo> selectByTel(@PathVariable Long tel,
                              @CookieValue("userTicket") String userTicket){
        Result<UserVo> result = userService.selectByTel(userTicket,tel);
        return result;
    }

    @ResponseBody
    @GetMapping("/select/id/{id}")
    public Result<UserVo> selectById(@PathVariable Long id){
        Result<UserVo> result = userService.selectById(id);
        return result;
    }

    @ResponseBody
    @PostMapping("/update/password")
    public Result updatePassword(@CookieValue("userTicket") String userTicket,
                                 @RequestParam("oldPassword") String oldPassword,
                                 @RequestParam("newPassword") String newPassword)
            throws NoSuchFieldException, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        Result result = userService.updatePassword(userTicket, oldPassword, newPassword);
        return result;
    }


    @ResponseBody
    @PostMapping("/update/{memberName}")
    public Result update(@CookieValue("userTicket") String userTicket,
                         @PathVariable String memberName,
                         @RequestParam Object value)
            throws NoSuchFieldException, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        Result result = userService.updateInfo(userTicket, memberName, value);
        return result;
    }

    @ResponseBody
    @PostMapping("/update/noCookie/{memberName}/{userTicket}/{value}")
    public <T> Result updateNoCookie(@PathVariable("userTicket") String userTicket,
                         @PathVariable String memberName,
                         @PathVariable T value)
            throws NoSuchFieldException, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        Result result = userService.updateInfo(userTicket, memberName, value);
        return result;
    }

}
